﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using suduku;

namespace Sudukux
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void operateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBox[] box = new TextBox[81];
            //bound the textBox from left to right
            //1st row
            box[0] = textBox1;
            box[1] = textBox2;
            box[2] = textBox3;
            box[3] = textBox4;
            box[4] = textBox5;
            box[5] = textBox6;
            box[6] = textBox7;
            box[7] = textBox8;
            box[8] = textBox9;
            //2nd row
            box[9] = textBox18;
            box[10] = textBox17;
            box[11] = textBox16;
            box[12] = textBox15;
            box[13] = textBox14;
            box[14] = textBox13;
            box[15] = textBox12;
            box[16] = textBox11;
            box[17] = textBox10;
            //3rd row
            box[18] = textBox27;
            box[19] = textBox26;
            box[20] = textBox25;
            box[21] = textBox24;
            box[22] = textBox23;
            box[23] = textBox22;
            box[24] = textBox21;
            box[25] = textBox20;
            box[26] = textBox19;
            //4th row
            box[27] = textBox36;
            box[28] = textBox35;
            box[29] = textBox34;
            box[30] = textBox33;
            box[31] = textBox32;
            box[32] = textBox31;
            box[33] = textBox30;
            box[34] = textBox29;
            box[35] = textBox28;
            //5th row
            box[36] = textBox45;
            box[37] = textBox44;
            box[38] = textBox43;
            box[39] = textBox42;
            box[40] = textBox41;
            box[41] = textBox40;
            box[42] = textBox39;
            box[43] = textBox38;
            box[44] = textBox37;
            //6th row
            box[45] = textBox54;
            box[46] = textBox53;
            box[47] = textBox52;
            box[48] = textBox51;
            box[49] = textBox50;
            box[50] = textBox49;
            box[51] = textBox48;
            box[52] = textBox47;
            box[53] = textBox46;
            //7th row
            box[54] = textBox63;
            box[55] = textBox62;
            box[56] = textBox61;
            box[57] = textBox60;
            box[58] = textBox59;
            box[59] = textBox58;
            box[60] = textBox57;
            box[61] = textBox56;
            box[62] = textBox55;
            //8th row
            box[63] = textBox72;
            box[64] = textBox71;
            box[65] = textBox70;
            box[66] = textBox69;
            box[67] = textBox68;
            box[68] = textBox67;
            box[69] = textBox66;
            box[70] = textBox65;
            box[71] = textBox64;
            //9th row
            box[72] = textBox81;
            box[73] = textBox80;
            box[74] = textBox79;
            box[75] = textBox78;
            box[76] = textBox77;
            box[77] = textBox76;
            box[78] = textBox75;
            box[79] = textBox74;
            box[80] = textBox73;

            ArrayList list1 = new ArrayList();
            ArrayList list2 = new ArrayList();
            ArrayList list3 = new ArrayList();
            int k = 0, i, j;
            int sign = 0;
            for (i = 0; i < 81; i++)
            {
                if (box[i].Text != "")
                {
                    sign = 1;
                    k++;
                    list1.Add(i / 9 + 1);
                    list2.Add(i % 9 + 1);
                    list3.Add(Convert.ToInt32(box[i].Text));
                }
            }
            if (sign != 0)
            {
                int[,] dd = new int[k, 3];
                j = 0;
                for (i = 0; i < k; i++)
                {
                    dd[i, j] = (int)list1[i];
                }
                j = 1;
                for (i = 0; i < k; i++)
                {
                    dd[i, j] = (int)list2[i];
                }
                j = 2;
                for (i = 0; i < k; i++)
                {
                    dd[i, j] = (int)list3[i];
                }
                int mm = 3;
                suduku.sudoku su = new suduku.sudoku();
                //   MatrixS.MatrixS su1 = new MatrixS.MatrixS();
                MathWorks.MATLAB.NET.Arrays.MWNumericArray d = new MathWorks.MATLAB.NET.Arrays.MWNumericArray(dd);
                MathWorks.MATLAB.NET.Arrays.MWNumericArray m = (MathWorks.MATLAB.NET.Arrays.MWNumericArray)mm;
                MWArray S = su.sudokux(m, d);
                double[,] SInt = (double[,])S.ToArray();
                k = 0;
                for (i = 0; i < 9; i++)
                {
                    for (j = 0; j < 9; j++)
                    {
                        box[k++].Text = ((int)SInt[i, j]).ToString();
                    }
                }
                for (i = 0; i < 81; i++)
                {
                    box[i].ReadOnly = true;
                }
            }
            else 
            {
                Warning w1 = new Warning();
                w1.ShowDialog();
            }
        }

        private void Form3_Load(object sender, EventArgs e)
        {

        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            TextBox[] box = new TextBox[81];
            //bound the textBox from left to right
            //1st row
            box[0] = textBox1;
            box[1] = textBox2;
            box[2] = textBox3;
            box[3] = textBox4;
            box[4] = textBox5;
            box[5] = textBox6;
            box[6] = textBox7;
            box[7] = textBox8;
            box[8] = textBox9;
            //2nd row
            box[9] = textBox18;
            box[10] = textBox17;
            box[11] = textBox16;
            box[12] = textBox15;
            box[13] = textBox14;
            box[14] = textBox13;
            box[15] = textBox12;
            box[16] = textBox11;
            box[17] = textBox10;
            //3rd row
            box[18] = textBox27;
            box[19] = textBox26;
            box[20] = textBox25;
            box[21] = textBox24;
            box[22] = textBox23;
            box[23] = textBox22;
            box[24] = textBox21;
            box[25] = textBox20;
            box[26] = textBox19;
            //4th row
            box[27] = textBox36;
            box[28] = textBox35;
            box[29] = textBox34;
            box[30] = textBox33;
            box[31] = textBox32;
            box[32] = textBox31;
            box[33] = textBox30;
            box[34] = textBox29;
            box[35] = textBox28;
            //5th row
            box[36] = textBox45;
            box[37] = textBox44;
            box[38] = textBox43;
            box[39] = textBox42;
            box[40] = textBox41;
            box[41] = textBox40;
            box[42] = textBox39;
            box[43] = textBox38;
            box[44] = textBox37;
            //6th row
            box[45] = textBox54;
            box[46] = textBox53;
            box[47] = textBox52;
            box[48] = textBox51;
            box[49] = textBox50;
            box[50] = textBox49;
            box[51] = textBox48;
            box[52] = textBox47;
            box[53] = textBox46;
            //7th row
            box[54] = textBox63;
            box[55] = textBox62;
            box[56] = textBox61;
            box[57] = textBox60;
            box[58] = textBox59;
            box[59] = textBox58;
            box[60] = textBox57;
            box[61] = textBox56;
            box[62] = textBox55;
            //8th row
            box[63] = textBox72;
            box[64] = textBox71;
            box[65] = textBox70;
            box[66] = textBox69;
            box[67] = textBox68;
            box[68] = textBox67;
            box[69] = textBox66;
            box[70] = textBox65;
            box[71] = textBox64;
            //9th row
            box[72] = textBox81;
            box[73] = textBox80;
            box[74] = textBox79;
            box[75] = textBox78;
            box[76] = textBox77;
            box[77] = textBox76;
            box[78] = textBox75;
            box[79] = textBox74;
            box[80] = textBox73;
            int i;
            for (i = 0; i < 81; i++)
            {
                box[i].Text = "";
            }
            for (i = 0; i < 81; i++)
            {
                box[i].ReadOnly = false;
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About a1 = new About();
            a1.ShowDialog();
        }

        private void sudokuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Help h1 = new Help();
            h1.ShowDialog();
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            TextBox[] box = new TextBox[81];
            //bound the textBox from left to right
            //1st row
            box[0] = textBox1;
            box[1] = textBox2;
            box[2] = textBox3;
            box[3] = textBox4;
            box[4] = textBox5;
            box[5] = textBox6;
            box[6] = textBox7;
            box[7] = textBox8;
            box[8] = textBox9;
            //2nd row
            box[9] = textBox18;
            box[10] = textBox17;
            box[11] = textBox16;
            box[12] = textBox15;
            box[13] = textBox14;
            box[14] = textBox13;
            box[15] = textBox12;
            box[16] = textBox11;
            box[17] = textBox10;
            //3rd row
            box[18] = textBox27;
            box[19] = textBox26;
            box[20] = textBox25;
            box[21] = textBox24;
            box[22] = textBox23;
            box[23] = textBox22;
            box[24] = textBox21;
            box[25] = textBox20;
            box[26] = textBox19;
            //4th row
            box[27] = textBox36;
            box[28] = textBox35;
            box[29] = textBox34;
            box[30] = textBox33;
            box[31] = textBox32;
            box[32] = textBox31;
            box[33] = textBox30;
            box[34] = textBox29;
            box[35] = textBox28;
            //5th row
            box[36] = textBox45;
            box[37] = textBox44;
            box[38] = textBox43;
            box[39] = textBox42;
            box[40] = textBox41;
            box[41] = textBox40;
            box[42] = textBox39;
            box[43] = textBox38;
            box[44] = textBox37;
            //6th row
            box[45] = textBox54;
            box[46] = textBox53;
            box[47] = textBox52;
            box[48] = textBox51;
            box[49] = textBox50;
            box[50] = textBox49;
            box[51] = textBox48;
            box[52] = textBox47;
            box[53] = textBox46;
            //7th row
            box[54] = textBox63;
            box[55] = textBox62;
            box[56] = textBox61;
            box[57] = textBox60;
            box[58] = textBox59;
            box[59] = textBox58;
            box[60] = textBox57;
            box[61] = textBox56;
            box[62] = textBox55;
            //8th row
            box[63] = textBox72;
            box[64] = textBox71;
            box[65] = textBox70;
            box[66] = textBox69;
            box[67] = textBox68;
            box[68] = textBox67;
            box[69] = textBox66;
            box[70] = textBox65;
            box[71] = textBox64;
            //9th row
            box[72] = textBox81;
            box[73] = textBox80;
            box[74] = textBox79;
            box[75] = textBox78;
            box[76] = textBox77;
            box[77] = textBox76;
            box[78] = textBox75;
            box[79] = textBox74;
            box[80] = textBox73;

            int i;
            for (i = 0; i < 81; i++)
            {
                box[i].Text = "";
            }
            for (i = 0; i < 81; i++)
            {
                box[i].ReadOnly = false;
            }

            box[2].Text="8";
            box[3].Text="3";
            box[5].Text="9";
            box[6].Text="1";
            box[9].Text="9";
            box[13].Text="6";
            box[17].Text="4";
            box[20].Text="7";
            box[21].Text="5";
            box[23].Text="4";
            box[24].Text="8";
            box[28].Text="3";
            box[29].Text="6";
            box[33].Text="5";
            box[34].Text="4";
            box[38].Text="1";
            box[42].Text="6";
            box[46].Text="4";
            box[47].Text="2";
            box[51].Text="9";
            box[52].Text="7";
            box[56].Text="5";
            box[57].Text="9";
            box[59].Text="7";
            box[60].Text="3";
            box[63].Text="6";
            box[67].Text="1";
            box[71].Text="8";
            box[74].Text="4";
            box[75].Text="6";
            box[77].Text="8";
            box[78].Text="2";
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            TextBox[] box = new TextBox[81];
            //bound the textBox from left to right
            //1st row
            box[0] = textBox1;
            box[1] = textBox2;
            box[2] = textBox3;
            box[3] = textBox4;
            box[4] = textBox5;
            box[5] = textBox6;
            box[6] = textBox7;
            box[7] = textBox8;
            box[8] = textBox9;
            //2nd row
            box[9] = textBox18;
            box[10] = textBox17;
            box[11] = textBox16;
            box[12] = textBox15;
            box[13] = textBox14;
            box[14] = textBox13;
            box[15] = textBox12;
            box[16] = textBox11;
            box[17] = textBox10;
            //3rd row
            box[18] = textBox27;
            box[19] = textBox26;
            box[20] = textBox25;
            box[21] = textBox24;
            box[22] = textBox23;
            box[23] = textBox22;
            box[24] = textBox21;
            box[25] = textBox20;
            box[26] = textBox19;
            //4th row
            box[27] = textBox36;
            box[28] = textBox35;
            box[29] = textBox34;
            box[30] = textBox33;
            box[31] = textBox32;
            box[32] = textBox31;
            box[33] = textBox30;
            box[34] = textBox29;
            box[35] = textBox28;
            //5th row
            box[36] = textBox45;
            box[37] = textBox44;
            box[38] = textBox43;
            box[39] = textBox42;
            box[40] = textBox41;
            box[41] = textBox40;
            box[42] = textBox39;
            box[43] = textBox38;
            box[44] = textBox37;
            //6th row
            box[45] = textBox54;
            box[46] = textBox53;
            box[47] = textBox52;
            box[48] = textBox51;
            box[49] = textBox50;
            box[50] = textBox49;
            box[51] = textBox48;
            box[52] = textBox47;
            box[53] = textBox46;
            //7th row
            box[54] = textBox63;
            box[55] = textBox62;
            box[56] = textBox61;
            box[57] = textBox60;
            box[58] = textBox59;
            box[59] = textBox58;
            box[60] = textBox57;
            box[61] = textBox56;
            box[62] = textBox55;
            //8th row
            box[63] = textBox72;
            box[64] = textBox71;
            box[65] = textBox70;
            box[66] = textBox69;
            box[67] = textBox68;
            box[68] = textBox67;
            box[69] = textBox66;
            box[70] = textBox65;
            box[71] = textBox64;
            //9th row
            box[72] = textBox81;
            box[73] = textBox80;
            box[74] = textBox79;
            box[75] = textBox78;
            box[76] = textBox77;
            box[77] = textBox76;
            box[78] = textBox75;
            box[79] = textBox74;
            box[80] = textBox73;

            int i;
            for (i = 0; i < 81; i++)
            {
                box[i].Text = "";
            }
            for (i = 0; i < 81; i++)
            {
                box[i].ReadOnly = false;
            }

            box[1].Text = "3";
            box[5].Text = "9";
            box[8].Text = "7";
            box[10].Text = "1";
            box[13].Text = "8";
            box[21].Text = "1";
            box[25].Text = "9";
            box[29].Text = "4";
            box[30].Text = "9";
            box[32].Text = "5";
            box[35].Text = "6";
            box[37].Text = "2";
            box[43].Text = "1";
            box[45].Text = "5";
            box[48].Text = "6";
            box[50].Text = "7";
            box[51].Text = "4";
            box[55].Text = "5";
            box[59].Text = "1";
            box[67].Text = "4";
            box[70].Text = "2";
            box[72].Text = "7";
            box[75].Text = "5";
            box[79].Text = "3";
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            TextBox[] box = new TextBox[81];
            //bound the textBox from left to right
            //1st row
            box[0] = textBox1;
            box[1] = textBox2;
            box[2] = textBox3;
            box[3] = textBox4;
            box[4] = textBox5;
            box[5] = textBox6;
            box[6] = textBox7;
            box[7] = textBox8;
            box[8] = textBox9;
            //2nd row
            box[9] = textBox18;
            box[10] = textBox17;
            box[11] = textBox16;
            box[12] = textBox15;
            box[13] = textBox14;
            box[14] = textBox13;
            box[15] = textBox12;
            box[16] = textBox11;
            box[17] = textBox10;
            //3rd row
            box[18] = textBox27;
            box[19] = textBox26;
            box[20] = textBox25;
            box[21] = textBox24;
            box[22] = textBox23;
            box[23] = textBox22;
            box[24] = textBox21;
            box[25] = textBox20;
            box[26] = textBox19;
            //4th row
            box[27] = textBox36;
            box[28] = textBox35;
            box[29] = textBox34;
            box[30] = textBox33;
            box[31] = textBox32;
            box[32] = textBox31;
            box[33] = textBox30;
            box[34] = textBox29;
            box[35] = textBox28;
            //5th row
            box[36] = textBox45;
            box[37] = textBox44;
            box[38] = textBox43;
            box[39] = textBox42;
            box[40] = textBox41;
            box[41] = textBox40;
            box[42] = textBox39;
            box[43] = textBox38;
            box[44] = textBox37;
            //6th row
            box[45] = textBox54;
            box[46] = textBox53;
            box[47] = textBox52;
            box[48] = textBox51;
            box[49] = textBox50;
            box[50] = textBox49;
            box[51] = textBox48;
            box[52] = textBox47;
            box[53] = textBox46;
            //7th row
            box[54] = textBox63;
            box[55] = textBox62;
            box[56] = textBox61;
            box[57] = textBox60;
            box[58] = textBox59;
            box[59] = textBox58;
            box[60] = textBox57;
            box[61] = textBox56;
            box[62] = textBox55;
            //8th row
            box[63] = textBox72;
            box[64] = textBox71;
            box[65] = textBox70;
            box[66] = textBox69;
            box[67] = textBox68;
            box[68] = textBox67;
            box[69] = textBox66;
            box[70] = textBox65;
            box[71] = textBox64;
            //9th row
            box[72] = textBox81;
            box[73] = textBox80;
            box[74] = textBox79;
            box[75] = textBox78;
            box[76] = textBox77;
            box[77] = textBox76;
            box[78] = textBox75;
            box[79] = textBox74;
            box[80] = textBox73;

            int i;
            for (i = 0; i < 81; i++)
            {
                box[i].Text = "";
            }
            for (i = 0; i < 81; i++)
            {
                box[i].ReadOnly = false;
            }

            box[0].Text = "5";
            box[4].Text = "4";
            box[8].Text = "9";
            box[11].Text = "8";
            box[12].Text = "1";
            box[14].Text = "5";
            box[15].Text = "4";
            box[19].Text = "3";
            box[25].Text = "5";
            box[28].Text = "8";
            box[30].Text = "3";
            box[32].Text = "7";
            box[34].Text = "1";
            box[36].Text = "2";
            box[44].Text = "3";
            box[46].Text = "7";
            box[48].Text = "6";
            box[50].Text = "2";
            box[52].Text = "4";
            box[55].Text = "1";
            box[61].Text = "9";
            box[65].Text = "7";
            box[66].Text = "2";
            box[68].Text = "9";
            box[69].Text = "8";
            box[72].Text = "8";
            box[76].Text = "7";
            box[80].Text = "4";
        }

        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            TextBox[] box = new TextBox[81];
            //bound the textBox from left to right
            //1st row
            box[0] = textBox1;
            box[1] = textBox2;
            box[2] = textBox3;
            box[3] = textBox4;
            box[4] = textBox5;
            box[5] = textBox6;
            box[6] = textBox7;
            box[7] = textBox8;
            box[8] = textBox9;
            //2nd row
            box[9] = textBox18;
            box[10] = textBox17;
            box[11] = textBox16;
            box[12] = textBox15;
            box[13] = textBox14;
            box[14] = textBox13;
            box[15] = textBox12;
            box[16] = textBox11;
            box[17] = textBox10;
            //3rd row
            box[18] = textBox27;
            box[19] = textBox26;
            box[20] = textBox25;
            box[21] = textBox24;
            box[22] = textBox23;
            box[23] = textBox22;
            box[24] = textBox21;
            box[25] = textBox20;
            box[26] = textBox19;
            //4th row
            box[27] = textBox36;
            box[28] = textBox35;
            box[29] = textBox34;
            box[30] = textBox33;
            box[31] = textBox32;
            box[32] = textBox31;
            box[33] = textBox30;
            box[34] = textBox29;
            box[35] = textBox28;
            //5th row
            box[36] = textBox45;
            box[37] = textBox44;
            box[38] = textBox43;
            box[39] = textBox42;
            box[40] = textBox41;
            box[41] = textBox40;
            box[42] = textBox39;
            box[43] = textBox38;
            box[44] = textBox37;
            //6th row
            box[45] = textBox54;
            box[46] = textBox53;
            box[47] = textBox52;
            box[48] = textBox51;
            box[49] = textBox50;
            box[50] = textBox49;
            box[51] = textBox48;
            box[52] = textBox47;
            box[53] = textBox46;
            //7th row
            box[54] = textBox63;
            box[55] = textBox62;
            box[56] = textBox61;
            box[57] = textBox60;
            box[58] = textBox59;
            box[59] = textBox58;
            box[60] = textBox57;
            box[61] = textBox56;
            box[62] = textBox55;
            //8th row
            box[63] = textBox72;
            box[64] = textBox71;
            box[65] = textBox70;
            box[66] = textBox69;
            box[67] = textBox68;
            box[68] = textBox67;
            box[69] = textBox66;
            box[70] = textBox65;
            box[71] = textBox64;
            //9th row
            box[72] = textBox81;
            box[73] = textBox80;
            box[74] = textBox79;
            box[75] = textBox78;
            box[76] = textBox77;
            box[77] = textBox76;
            box[78] = textBox75;
            box[79] = textBox74;
            box[80] = textBox73;

            int i;
            for (i = 0; i < 81; i++)
            {
                box[i].Text = "";
            }
            for (i = 0; i < 81; i++)
            {
                box[i].ReadOnly = false;
            }
            box[4].Text = "9";
            box[10].Text = "6";
            box[12].Text = "5";
            box[13].Text = "7";
            box[14].Text = "2";
            box[17].Text = "9";
            box[18].Text = "2";
            box[20].Text = "7";
            box[21].Text = "3";
            box[27].Text = "5";
            box[33].Text = "1";
            box[36].Text = "1";
            box[37].Text = "2";
            box[43].Text = "6";
            box[44].Text = "7";
            box[47].Text = "8";
            box[53].Text = "3";
            box[59].Text = "3";
            box[60].Text = "6";
            box[62].Text = "4";
            box[63].Text = "9";
            box[66].Text = "8";
            box[67].Text = "2";
            box[68].Text = "4";
            box[70].Text = "7";
            box[76].Text = "1";
        }
    }
}
